const storagefn = global?.localStorage || {};
export default {
  get: (key: string) => {
    return JSON.parse(storagefn?.getItem?.(key)! || '{}');
  },
  set: (key: string, data: any) => {
    return storagefn?.setItem?.(key, JSON.stringify(data));
  },
  remove: (key: string) => {
    storagefn?.removeItem?.(key);
  },
  add: (parentKey: string, key: string, data: any) => {
    storagefn?.getItem?.(parentKey);
    const parentData = JSON.parse(storagefn?.getItem(parentKey)!);
    if (parentData) {
      parentData.key = data;
    }
    storagefn?.setItem?.(parentKey, JSON.stringify(data));
    storagefn?.removeItem?.(key);
  },
  clear: () => {
    storagefn?.clear();
  },
};
